home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / basics / show movie / menustuff.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  12.2 KB  |  464 lines

  1. /*
  2.     File:        MenuStuff.c
  3.  
  4.     Contains:        Menu handling routines.
  5.                             Creates and destroys the menu bar
  6.                             Responds to user selecting menu item (mouse or keyboard)
  7.                             Does all the calls to open a single movie or a master
  8.                             and slave 
  9.  
  10.     Written by: Jason Hodges-Harris & Don Swatman    
  11.  
  12.     Copyright:    Copyright © 1995-1999 by Apple Computer, Inc., All Rights Reserved.
  13.  
  14.                 You may incorporate this Apple sample source code into your program(s) without
  15.                 restriction. This Apple sample source code has been provided "AS IS" and the
  16.                 responsibility for its operation is yours. You are not permitted to redistribute
  17.                 this Apple sample source code as "Apple sample source code" after having made
  18.                 changes. If you're going to re-distribute the source, we require that you make
  19.                 it clear in the source that the code was descended from Apple sample source
  20.                 code, but that you've made changes.
  21.  
  22.     Change History (most recent first):
  23.                 8/17/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  24.                 
  25.  
  26. */
  27.  
  28.  
  29. #include <Fonts.h>
  30. #include <Devices.h>
  31. #include <ToolUtils.h>
  32.  
  33. #include "MenuStuff.h"
  34.  
  35. #include "WindStuff.h"
  36. #include "MovieStuff.h"
  37. #include "MoviePrefs.h"
  38.  
  39. //----------------------------------------------
  40. // Define menubar constants
  41. //----------------------------------------------
  42.  
  43. #define    kMenuBar        128
  44.  
  45. #define mApple    128
  46. #define iAbout            1
  47.  
  48. #define    mFile    129
  49. #define iOpen             1
  50. #define iOpenMS         2
  51. #define iClose     3
  52. #define iQuit      5
  53.  
  54. #define mEdit   130
  55. #define iUndo            1
  56. #define iCut            3
  57. #define    iCopy            4
  58. #define iPaste        5
  59.  
  60.  
  61. //----------------------------------------------
  62. // Create the menu bar
  63. //----------------------------------------------
  64.  
  65. void MenuBarInit (void)
  66. {
  67.     Handle        menuBar;
  68.     MenuHandle    hMenu;
  69.     
  70. // Create the menu bar
  71.     menuBar = GetNewMBar(kMenuBar);  // Get a menu bar resource 
  72.     SetMenuBar (menuBar);            // Set the current menu bar to it 
  73.  
  74.     hMenu = GetMenuHandle(mApple); // Get a handle to the apple Menu
  75.     AppendResMenu (hMenu,'DRVR');  // Attach the Desk Accessories on to the end
  76.     
  77.     DrawMenuBar();    // Ok, draw the menu bar
  78. }
  79.  
  80. //==============================================
  81. // Various menu bar utilities
  82. //==============================================
  83.  
  84. //----------------------------------------------
  85. // SetMenuItemEnabled
  86. //
  87. // Grays or Un grays a menu item
  88. // Returns true if something was enabled
  89. //----------------------------------------------
  90.  
  91. Boolean SetMenuItemEnabled ( short menuID,
  92.                                                          short theItem,
  93.                                                          Boolean enableState );
  94. Boolean SetMenuItemEnabled ( short menuID,
  95.                                                          short theItem,
  96.                                                          Boolean enableState )
  97. {
  98.     MenuHandle    hMenu;
  99.  
  100.     hMenu = GetMenuHandle(menuID);
  101.     if (enableState)
  102.         EnableItem(hMenu, theItem);
  103.     else
  104.         DisableItem(hMenu, theItem);
  105.     return ( enableState );
  106. }
  107.  
  108. //----------------------------------------------
  109. // DoAdjustFileMenu
  110. //
  111. // Sets each item in the file menu
  112. //----------------------------------------------
  113.  
  114. static Boolean    DoAdjustFileMenu(WindowPtr window);
  115. static Boolean    DoAdjustFileMenu(WindowPtr window)
  116. {
  117.     short newWind;          // throw away value used by IsFreeWind
  118.     Boolean menuEnabled;    // true if any item is enabled
  119.     Boolean redrawMenuBar;  // true if we need to redraw the menu bar
  120.     static
  121.             Boolean    fileMenuEnabled = true; // This is a static so it'll hang around
  122.                                                                             // Reflects whither the file menu is currently
  123.                                                                             // grayed(false) or enabled(true)
  124.  
  125. // Open needs just one free window 
  126.     menuEnabled  = SetMenuItemEnabled( mFile, iOpen,   IsFreeWind(&newWind, 1) );
  127.  
  128. // Open master and slave needs 2 free windows
  129.     menuEnabled |= SetMenuItemEnabled( mFile, iOpenMS, IsFreeWind(&newWind, 2) );
  130.  
  131. // Close is ok if it's our window
  132.     menuEnabled |= SetMenuItemEnabled( mFile, iClose,  IsOurWindow( window ) );
  133.  
  134. // Quit always enabled
  135.     menuEnabled |= SetMenuItemEnabled( mFile, iQuit,   true);
  136.  
  137. // If nothing enabled the change then menu title to grayed out
  138.     if (!menuEnabled)
  139.         menuEnabled = SetMenuItemEnabled( mFile, 0,  false);
  140.     
  141. // If menu title has changed, then return the fact that we have
  142. //   to redraw the menu bar
  143.     redrawMenuBar   = (fileMenuEnabled != menuEnabled);
  144.     fileMenuEnabled = menuEnabled;
  145.  
  146.     return(redrawMenuBar);
  147. }
  148.  
  149. //----------------------------------------------
  150. // DoAdjustEditMenu
  151. //
  152. // As we don't do use this menu, we just gray
  153. // the whole lot out
  154. //----------------------------------------------
  155.  
  156. static Boolean    DoAdjustEditMenu(WindowPtr window )
  157. {
  158. #pragma unused ( window )
  159.  
  160.     Boolean            menuEnabled;    // true if any item is enabled
  161.     Boolean            redrawMenuBar;  // true if we need to redraw the menu bar
  162.     short              itemCount;      // count down each menu item
  163.     static 
  164.             Boolean    editMenuEnabled = true; // This is a static so it'll hang around
  165.                                                                             // Reflects whither the file menu is currently
  166.                                                                             // grayed(false) or enabled(true)
  167.  
  168. // Gray out all the items
  169.     menuEnabled = false;
  170.     for (itemCount = iUndo; itemCount <= iPaste; ++itemCount)
  171.         menuEnabled |= SetMenuItemEnabled(mEdit, itemCount, false);
  172.  
  173. // If nothing enabled then change the menu title to grayed out
  174.     if (!menuEnabled)
  175.         menuEnabled |= SetMenuItemEnabled( mEdit, 0,  false);
  176.  
  177. // If menu title has changed, then return the fact that we have
  178. //   to redraw the menu bar
  179.     redrawMenuBar   = (editMenuEnabled != menuEnabled);
  180.     editMenuEnabled = menuEnabled;
  181.     return(redrawMenuBar);
  182. }
  183.  
  184. //----------------------------------------------
  185. // DoAdjustMenus
  186. //
  187. // Adjust all the menu items and redraws the menu 
  188. // bar if something has changed
  189. //----------------------------------------------
  190.  
  191. void    DoAdjustMenus(void)
  192. {
  193.     WindowPtr    window;
  194.     Boolean   redrawMenuBar;
  195.  
  196.     window = FrontWindow();
  197.  
  198.     redrawMenuBar  = DoAdjustFileMenu ( window );  // Adjust the file menu
  199.     redrawMenuBar |= DoAdjustEditMenu ( window );  // Adjust the edit menu
  200.  
  201. // redraw the menu bar if one of the menu titles has changed
  202.     if (redrawMenuBar)
  203.         DrawMenuBar();
  204. }
  205.  
  206.  
  207. //----------------------------------------------
  208. // DoSlaveMovieTest
  209. //
  210. // Open two movies and slave one of the other
  211. //----------------------------------------------
  212.  
  213. void DoSlaveMovieTest ( void );
  214. void DoSlaveMovieTest ( void )
  215. {
  216.     OSErr   theErr = noErr;
  217.     short   masterNum = -1;     // window number of master window
  218.     short   slaveNum  = -1;     // window number of slave window
  219.  
  220. // Check to see if there is room for 2 movies
  221.     if (IsFreeWind(&masterNum, 2))
  222.     {
  223. // Let the user change any of the preferences
  224.         theErr = OneMoviePref( &gDefaultMoviePrefs, true );
  225.         if (!theErr)
  226.         {
  227.     
  228.     // Create the master movie in the master Window
  229.             CreateWindow( masterNum, "\pMaster Movie", kWindAtFront );
  230.             if (gTheWinds[masterNum])
  231.             {
  232.     // Create a movie
  233.                 theErr = OpenMovieWindow ( gTheWinds[masterNum],  // WindowPtr
  234.                                                                      kUserCloseWind,        // Force user to close at end
  235.                                                                      gDefaultMoviePrefs.hasController ); // Add controller if
  236.                                                                                                                                               // user wants it
  237.                 if (!theErr)
  238.                 {
  239.     
  240.     // Create the slave movie Window
  241.                     if (IsFreeWind(&slaveNum, 1))
  242.                     {
  243.                         CreateWindow( slaveNum, "\pSlave Movie", gTheWinds[masterNum] );
  244.                         if (gTheWinds[slaveNum])
  245.                         {
  246.     // Create a movie in the slave window
  247.                             theErr = OpenMovieWindow ( gTheWinds[slaveNum],  // WindowPtr
  248.                                                                                  kUserCloseWind,       // Force user to close at end
  249.                                                                                  kNoMovieController ); // No Movie Controller
  250.     
  251.     // Set up any changes to the masters movies rate
  252.                             if (!theErr)
  253.                                 if (gDefaultMoviePrefs.rateChangeDelay)
  254.                                     theErr = SetupMovieRate( gTheWinds[masterNum],  gDefaultMoviePrefs.rateChangeDelay  );
  255.                                 
  256.     // Set up the looping in the master movie - 20 seconds from start go back to 10 secs
  257.                             if (!theErr)
  258.                                 if (gDefaultMoviePrefs.do20to10Loop)
  259.                                     theErr = SetupLoop( gTheWinds[masterNum], 20, 10  );
  260.                 
  261.     // Now hook the two movies together
  262.                             if (!theErr)
  263.                                 theErr = SetupSlaveMovie( gTheWinds[masterNum],
  264.                                                                                     gTheWinds[slaveNum],
  265.                                                                                     gDefaultMoviePrefs.slaveAheadBy,
  266.                                                                                     gDefaultMoviePrefs.slaveStartDelay );
  267.  
  268.     // Display and order the windows
  269.                             if (!theErr)
  270.                             {
  271.                 // Show the windows
  272.                                 ShowWindow( gTheWinds[slaveNum] );
  273.                                 ShowWindow( gTheWinds[masterNum] );
  274.                 
  275.                 // Select the master window (as the front active window)
  276.                                 SelectWindow ( gTheWinds[masterNum] );
  277.             
  278.                 // Move slave window to be behind the master in the window list
  279.                                 SendBehind (gTheWinds[slaveNum], gTheWinds[masterNum]);
  280.  
  281.                             }    
  282.  
  283.                 // If we don't have a controller then start the movie
  284.                             if (!theErr)
  285.                                 if (!gDefaultMoviePrefs.hasController)
  286.                                     theErr = StartMovieWindow( gTheWinds[masterNum],kStartFromBegining );
  287.             
  288.                         }
  289.                     }
  290.                 }
  291.  
  292. // If we have an error, then close down the windows
  293.                 if (theErr)
  294.                 {
  295.                     if (masterNum != -1)
  296.                         CloseOurWindow( masterNum );
  297.                     if (slaveNum != -1)
  298.                         CloseOurWindow( slaveNum );
  299.                 }
  300.             }
  301.     // Adjust the menus, whatever happens
  302.             DoAdjustMenus();
  303.         }
  304.     }
  305. }
  306.  
  307.  
  308. //----------------------------------------------
  309. // DoOpenWindow
  310. //
  311. // Open one movie using the options setup
  312. //----------------------------------------------
  313.  
  314. void DoOpenWindow (void);
  315. void DoOpenWindow (void)
  316. {
  317.     OSErr   theErr;
  318.     short   windNum;    // The windows number in the array
  319.         
  320. // Check there is a free window
  321.     if (IsFreeWind(&windNum,1))
  322.     {
  323. // Get any options the user might want
  324.         theErr = OneMoviePref( &gDefaultMoviePrefs, false );
  325.         if (!theErr)
  326.         {
  327.  
  328. // Open a window
  329.             CreateWindow( windNum, "\pUntitled", kWindAtFront  );
  330.             if (gTheWinds[windNum])
  331.             {
  332.     // Create a movie
  333.                 theErr = OpenMovieWindow ( gTheWinds[windNum],                 // windowPtr
  334.                                                                      gDefaultMoviePrefs.closeAtEnd,      // Users option for auto close
  335.                                                                      gDefaultMoviePrefs.hasController ); // Users Option for controller
  336.                 
  337.     // Set up the movie rate changes
  338.                 if (!theErr)
  339.                     if (gDefaultMoviePrefs.rateChangeDelay)
  340.                         theErr = SetupMovieRate( gTheWinds[windNum],  gDefaultMoviePrefs.rateChangeDelay  );
  341.                     
  342.     // Set up the looping in the master movie - 20 seconds from start go back to 10 secs
  343.                 if (!theErr)
  344.                     if (gDefaultMoviePrefs.do20to10Loop)
  345.                         theErr = SetupLoop( gTheWinds[windNum],
  346.                                                                 gDefaultMoviePrefs.loopFrom, gDefaultMoviePrefs.loopTo  );
  347.                     
  348. // if no errors, then display the windows
  349.                 if (!theErr)
  350.                 {
  351.         // Show the windows
  352.                     ShowWindow(gTheWinds[windNum]);
  353.     
  354.         // Select the window (as the front active window)
  355.                     SelectWindow ( gTheWinds[windNum] );
  356.                 }    
  357.  
  358.     // If we don't have a controller then start the movie
  359.                 if (!theErr)
  360.                     if (!gDefaultMoviePrefs.hasController)
  361.                         theErr = StartMovieWindow( gTheWinds[windNum],kStartFromBegining );
  362.     
  363.     // If we've had an error, then remove the window and clean up
  364.                 if (theErr)
  365.                 {
  366.                     if (windNum != -1)
  367.                         CloseOurWindow( windNum );
  368.                 }
  369.             }    
  370.         }
  371. // Adjust the menus, whatever happens
  372.         DoAdjustMenus();
  373.     }
  374. }
  375.  
  376.  
  377. //----------------------------------------------
  378. // DoMenuCommand
  379. //
  380. // Handles selection of items in menu
  381. //----------------------------------------------
  382.  
  383. void    DoMenuCommand(long menuResult)
  384. {
  385.     short        menuID;
  386.     short        menuItem;
  387.     short        daRefNum;
  388.     Str255    daName;
  389.     short   windNum;
  390.     
  391. // convert menuResult into menu and item
  392.     menuID   = HiWord(menuResult);
  393.     menuItem = LoWord(menuResult);
  394.  
  395.     switch (menuID) 
  396.     {
  397.  
  398. // Apple Menu
  399.         case mApple:
  400.             switch (menuItem) 
  401.             {
  402.     // Display about box
  403.                 case iAbout:
  404.                     AboutBox();
  405.                     break;
  406.  
  407.     // Handle DA selected in Apple Menu
  408.                 default:
  409.                     GetMenuItemText(GetMenuHandle(mApple), menuItem, daName);
  410.                     daRefNum = OpenDeskAcc(daName);
  411.                     break;
  412.             }
  413.             break;
  414.  
  415. // File Menu
  416.         case mFile:
  417.             switch (menuItem) 
  418.             {
  419.  
  420.         // Open a movie window
  421.                 case iOpen:
  422.                     DoOpenWindow ();
  423.                     break;
  424.  
  425.         // Open a master and slave movie windows
  426.                 case iOpenMS:
  427.                     DoSlaveMovieTest( );                    
  428.                     break;
  429.         // Close front window
  430.                 case iClose:
  431.                     windNum = GetWindowNum ( FrontWindow() );
  432.                     if (windNum != -1 )
  433.                         CloseOurWindow( windNum );
  434.                     break;
  435.         // User wants to quit so set gDone to True
  436.                 case iQuit:
  437.                     gDone=true;     
  438.                     break;
  439.             }
  440.             break;
  441.  
  442. // Edit Menu
  443.     // Don't do anything
  444.         case mEdit:
  445.             switch (menuItem) 
  446.             {
  447.                 case iUndo:
  448.                     break;
  449.                 case iCut:
  450.                     break;
  451.                 case iCopy:
  452.                     break;
  453.                 case iPaste:
  454.                     break;
  455.             }
  456.         break;
  457.             
  458.     }
  459. // Un highlite the menu title
  460.     HiliteMenu(0);
  461. }
  462.  
  463.  
  464.